/**
 * @file{同步 汇总信息}
 */
 'use strict';
 
 const schedule = require('node-schedule');
 const LRU = require("lru-cache");
 const {initFlawFilterData} = require('../model/summary/flaw_filter');
 const {initPanelData} = require('../model/summary/panel_summary');
 const {initFlawMujinaData} = require('../model/summary/flaw');
 const options = {
    max: 500,
    length: function (n, key) { return n * 2 + key.length },
    dispose: function (key, n) { },
    maxAge: 1000 * 60 * 60
 }
 class SynchronizeMujian {

    constructor (tableModel, sequelize_aim, stationName) {
        //各个表的操作模型对象
        this.cache = new LRU(options);
        this.PanelMujian = tableModel.PanelMujian;
        this.FlawMujian = tableModel.FlawMujian;
        this.FlawFilterMujian = tableModel.FlawFilterMujian;
        this.sequelize_aim = sequelize_aim;
        this.stationName = stationName;

        // 初始化各个表
        initFlawFilterData(this.sequelize_aim);
        initPanelData(this.sequelize_aim);
        initFlawMujinaData(this.sequelize_aim);
    }
    /**
     * 跟新缺陷
     * @param {Array}
     */
    async updateFLawInfo (flawArr) {
        if (!flawArr.length) return false;
        //1, 查询汇总库的uid的跟新位置
        let maxUid = await this.FlawMujian.getMaxUidfromDb(this.sequelize_aim);
        //2，遍历数组处理uid
        let formattedFlawArray = flawArr.map((item, index) => {
            return {
                ...item,
                uid: maxUid + index + 1
            }
        })
        //3，上传到汇总库
        this.postDataToLineDb(this.sequelize_aim, formattedFlawArray, 'Flaw');
    }
    /**
     * 跟新缺陷分类
     * @param {Array} flawFilterArr
     */
    async updateFlawFilterInfo (flawFilterArr) {
        if (!flawFilterArr.length) return false;
        // 1,查询汇总库的最新同步位置
        let maxId = await this.FlawFilterMujian.getMaxIdfromDb(this.sequelize_aim);
        // 2,遍历数组处理uid
        let formattedFlawFilterArray = flawFilterArr.map((item, index) => {
            return {
                ...item,
                id: maxId + index + 1
            }
        })
        // 3,上传到汇总库
        this.postDataToLineDb(this.sequelize_aim, formattedFlawFilterArray, 'flaw_filter');
    }
    /**
     * 跟新panel信息
     * @param {Object} panelInfo
     */
     async updatePanelInfo (panelInfo) {
        if (!panelInfo.uid) return false;
        // 1,查询目标库panel表是否存在此panel
        let linePanelInfo = this.PanelMujian.getPanelInfoByPanelId(this.sequelize_aim, panelInfo.uid);
        // 2,存在则查出来求并，不存在直接上传
        if (linePanelInfo.length) {
            let panelSummary = {
                ...panelInfo,
                flaw_count: panelInfo.flaw_count + linePanelInfo.flaw_count,
                is_ok: panelInfo.is_ok && linePanelInfo.is_ok ? 1: 0
            }
            this.postDataToLineDb(this.sequelize_aim, panelSummary, 'Panel');
        } else {
            this.postDataToLineDb(this.sequelize_aim, panelSummary, 'Panel');
        }
    }
    /**
     * 上传到目标库
     * @param {*} operatorName
     * @param {*} data
     * @param {表名} tableName
     */
    async postDataToLineDb (operatorName, data, tableName) {
        // 存进目标库
        try {
            let res = await operatorName.uploadPolInfo(data);
            if(res.dataValues || res.length) {
                console.log(`${tableName}上传${this.stationName}库sucess!`);
            }else {
                console.log(`${tableName}上传${this.stationName}库的数据为空`);
            }
        } catch (error) {
            console.log(`${tableName}上传${this.stationName}库failed`);
        }
    }
    static getInstance (panel, sequelize_aim, stationName) {
        return new SynchronizeMujian(panel, sequelize_aim, stationName);
    }
 }

 module.exports = SynchronizeMujian;